package com.martian.controller;

import com.martian.common.exception.SysException;
import com.martian.common.util.ThreadLocalMap;
import com.martian.common.wrapper.ResultWrapper;
import com.martian.pojo.form.NewMovieQuestionFrom;
import com.martian.pojo.form.UserAnswerForm;
import com.martian.pojo.form.UserLoginForm;
import com.martian.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author martian
 * @version V1.0.0
 * @Description
 * @date 2023-08-14 12:38
 */
@CrossOrigin
@Api(value = "QuestionController", tags = {"知识问答API"})
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Resource
    private QuestionService questionService;

    @ApiOperation(value = "获取电影知识问答题", produces = "application/json")
    @GetMapping("/getMovieQuestions")
    public ResultWrapper getMovieQuestions(@RequestParam Integer movieId,@RequestParam Integer num){
        try {
            return ResultWrapper.successWithData(questionService.getMovieQuestions(movieId,num));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "新增电影知识问答题", produces = "application/json")
    @PostMapping("/insertMovieQuestions")
    public ResultWrapper insertMovieQuestions(@RequestBody NewMovieQuestionFrom newMovieQuestionFrom){
        try {
            Integer questionId = questionService.insertMovieQuestions(newMovieQuestionFrom);
            return ResultWrapper.successWithData(questionId);
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "新增用户知识问答记录", produces = "application/json")
    @PostMapping("/insertUserAnswer")
    public ResultWrapper insertUserAnswer(@RequestBody UserAnswerForm userAnswerForm){
        try {
            Integer userId = (Integer) ThreadLocalMap.get("THREAD_LOCAL_KEY_LOGIN_USER");
            questionService.insertUserAnswer(userId,userAnswerForm);
            return ResultWrapper.success();
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "修改用户知识问答记录", produces = "application/json")
    @PostMapping("/updateUserAnswer")
    public ResultWrapper updateUserAnswer(@RequestBody UserAnswerForm userAnswerForm){
        try {
            questionService.updateUserAnswer(userAnswerForm);
            return ResultWrapper.success();
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "查询用户是否具有评分某一部电影的资格", produces = "application/json")
    @GetMapping("/getUserRatingQualification")
    public ResultWrapper getUserRatingQualification(@RequestParam Integer movieId){
        try {
            Integer userId = (Integer) ThreadLocalMap.get("THREAD_LOCAL_KEY_LOGIN_USER");
            return ResultWrapper.successWithData(questionService.getRatingQualification(userId,movieId));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
}
